package problems.practice;

import java.util.HashMap;

/**
 * 面试题 16.02. 单词频率
 * <p>https://leetcode.cn/problems/words-frequency-lcci/</p>
 *
 * @author habitplus
 * @since 0:01, 2022/10/6
 */
public class Ms_T16_02 {
    /**
     * 字典树
     */
    class WordsFrequency {

        public WordsFrequency(String[] book) {

        }

        public int get(String word) {
            int ret = 0;

            return ret;
        }


    }


    /**
     * 哈希表
     */
    class WordsFrequency1 {

        private String[] book;

        private boolean isBuild;
        private HashMap<String, Integer> freqMap;

        public WordsFrequency1(String[] book) {
            this.book = book;
            freqMap = new HashMap<>();
            isBuild = false;
        }

        public int get(String word) {
            if (!isBuild) {
                buildDictMap();
                isBuild = true;
            }

            return freqMap.getOrDefault(word, 0);
        }

        private void buildDictMap() {
            if (book == null) return;

            for (String w : book) {
                if (w == null || w.length() < 1) continue;
                freqMap.put(w, freqMap.getOrDefault(w, 0) + 1);
            }
        }
    }
}


